package hammersmith.local.management;

import hammersmith.local.exceptions.ArticleNotFoundException;
import hammersmith.local.exceptions.WrongInputException;
import hammersmith.local.exceptions.WrongMinCountException;
import hammersmith.local.valueobjects.Article;
import hammersmith.local.valueobjects.Employee;
import hammersmith.local.valueobjects.MassArticle;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;

/**
 * @author hammersmith Article-Management-Klasse
 */
public class ArticleManagement implements Serializable {

	private static final long serialVersionUID = 908071990522479649L;
	private ArrayList<Article> articleList;

	// Konstruktor
	public ArticleManagement() {
		articleList = new ArrayList<Article>();
	}

	/**
	 * 
	 * Methode zum Hinzufuegen eines neuen Artikels
	 * 
	 * @param handler
	 *            : zum Speichern des Shops
	 * @param shop
	 *            : Shop-Objekt
	 * @param id
	 * @param name
	 * @param description
	 * @param stock
	 * @param size
	 * @param price
	 * @param color
	 * @param type
	 * @return
	 */
	public Article addNewArticle(Employee e, String name, String description,
			int stock, String size, float price, String color, String type,
			int minCount) throws WrongInputException, WrongMinCountException {

		// Anlegen eines neuen Artikelobjekts
		Timestamp timestamp;

		try {

			if (minCount > 1) {

				if (stock % minCount != 0) {
					throw new WrongMinCountException(minCount);
				}
			}

			Article article;

			if (minCount > 1) {
				article = new MassArticle(name, description, stock, size,
						price, color, type, minCount, "");
			} else {
				article = new Article(name, description, stock, size, price,
						color, type,"");
				//TODO: IMAGEPFAD ERSETZEN!
			}

			// hinzufuegen zur Liste
			this.articleList.add(article);

			return article;

		} catch (NumberFormatException ex) {
			// e.printStackTrace();
			System.out.println("...");
			throw new WrongInputException();
		}
	}

	public int deleteArticle(Employee e, Article a) {

		try {

			int stock = a.getStock();
			this.articleList.remove(a);

			return stock;

		} catch (Exception ex) {
			return 0;
		}
	}

	/**
	 * 
	 * Methode zum Ueberpruefen ob die Eingabe numerisch ist
	 * 
	 * @param str
	 *            : Wert den es zu ueberpruefen gilt
	 * @param format
	 * @return
	 */
	public boolean isNumeric(String str, String format) {
		try {

			if (format.equals("i")) {
				Integer i = Integer.parseInt(str);
			} else if (format.equals("f")) {
				float i = Float.parseFloat(str);
			} else if (format.equals("d")) {
				double i = Double.parseDouble(str);
			}

		} catch (NumberFormatException nfe) {
			return false;
		}
		return true;
	}

	/**
	 * 
	 * Methode zum bearbeiten eines Artikels
	 * 
	 * @param id
	 * @return
	 * @throws ArticleNotFoundException
	 */
	public Article editArticles(Article article, String type, String name, String size, String color, String stock, String price, String minCount, String description) {
		
		article.setType(type);
		article.setName(name);
		article.setSize(size);
		article.setColor(color);
		article.setStock(Integer.parseInt(stock));
		article.setPrice(Float.parseFloat(price));
		article.setDescription(description);

		if (article instanceof MassArticle) {
			MassArticle articleM = (MassArticle) article;
			articleM.setMinCount(Integer.parseInt(minCount));
		} else {
		}
		
		return article;
	}

	/**
	 * 
	 * Methode um ein Artikelobjekt aus der Liste per eingabe der Artikelnummer
	 * zu finden
	 * 
	 * @param id
	 * @return
	 */
	public Article getArticleById(long id) throws ArticleNotFoundException {
		for (Article article : this.articleList) {
			if (article.getId() == id) {
				return article;
			} else {

			}
		}
		throw new ArticleNotFoundException();
	}

	public ArrayList<Article> getArticleList() {
		return articleList;
	}

	public void setArticleList(ArrayList<Article> articleList) {
		this.articleList = articleList;
	}

}
